/*
Will contain login pulse/and support members,
- Hessinger
*/

objectdef obj_Login
{
	variable index:int ActiveDroneIDList
	variable int CategoryID_Drones = 18
	variable string Username
	variable string Password
	variable int CharID
	variable string CurrentState
	variable int Pulse
		
	method Initialize()
	{    
    echo "Obj_Login: Loaded"
	}
	
	method Shutdown()
	{
		echo "obj_Login: Shutting Down"
		Event[OnFrame]:DetachAtom[This:StatePulse]
	}
	
	method StatePulse()
	{
		Pulse:Inc
		variable int IntervalInSeconds = 2
		
		if ${Pulse} >= ${Math.Calc[${Display.FPS} * ${IntervalInSeconds}]}
		{
			if ((${Core.GameHour} == 12 && ${Core.GameMinute} <= 10) || (${Core.GameHour} == 11) || (${Core.GameHour} == 10 && ${Core.GameMinute} >= 50) && ${Me.Ship(exists)})
			{
				This.CurrentState:Set["LOGOUT"]
				FrameCounter:Set[0]
				return
			}
			if (${Core.GameHour} == 12 && ${Core.GameMinute} <= 10) || (${Core.GameHour} == 11) || (${Core.GameHour} == 10 && ${Core.GameMinute} >= 50)
			{
				This.CurrentState:Set["DOWNTIME"]
				FrameCounter:Set[0]
				return
			}
			if ${Login(exists)} 
			{
				This.CurrentState:Set["LOGINSCREEN"]
				FrameCounter:Set[0]
				return
			}
			if ${CharSelect(exists)}
			{
				This.CurrentState:Set["CHARSELECT"]
				FrameCounter:Set[0]
				return
			}
			if !${Script[EVEBot](exists)} && ${Me.Ship(exists)}
			{
				This.CurrentState:Set["STARTEVEBOT"]
				FrameCounter:Set[0]
				return
			}
			if ${Me.Ship(exists)}
			{
				This.CurrentState:Set["LOGGEDIN"]
				FrameCounter:Set[0]
				return
			}
			
			FrameCounter:Set[0]
			This.CurrentState:Set["OBJ_LOGIN_UNKNOWN_STATE"]
			
		}
	}
	
	function ProcessState()
	{
		do
		{
			switch ${This.CurrentState}
			{
				case DOWNTIME
					echo "We're at server downtime"
					;Add function to do a while loop within the range period of downtime rather then using waits.
					wait 500
					continue
				;this will be moved into the core's processor later.
				case LOGOUT
					call Core.ReturnAllToDroneBay
					call Core.Dock
					continue
				case LOGINSCREEN
					call This.Login
					continue
				case CHARSELECT
					call This.SelectChar
					continue
				case STARTEVEBOT
					call This.StartBot
					continue
				case LOGGEDIN
					;Call Core.AttachCore
					wait 10
					continue
				case OBJ_LOGIN_UNKNOWN_STATE
					echo "We have no state, must be an error, hopefully"
					wait 10
					continue
		}
	}
	while TRUE
}

	function Login()
	{
		echo "Attempting to login"
		EVE:CloseAllMessageBoxes
		wait 5
		Login:SetUsername[${This.Username}]
	  wait 5
	  Login:SetPassword[${This.Password}]
	  wait 5
	  Login:Connect
	  wait 200
	}	
	
	function SelectChar()
	{
		echo "At Char Select Screen"
		EVE:CloseAllMessageBoxes
		CharSelect:ClickCharacter[${This.CharID}]
		wait 240
	}
	
	;This function will be replaced.
	function StartBot()
	{
		echo "Starting Bot Routines"
		while ${Me.ToEntity.Mode} == 3
		{
			wait 10
		}
		
		if ${LoginScript.DronesInSpace} > 0 && !${Script[EVEBot](exists)}
	  {
	    call LoginScript.GetDrones
	  }
		
		if ${Script[EVEBot](exists)}
		{
			echo "Evebot already running"
		}
		else
		{
			do
			{
				runscript evebot/evebot
				wait 50
			}
			while !${Script[EVEBot](exists)}
			
			Script[EVEBot]:Resume
	    Script[EVEBot].VariableScope.play:Set[TRUE]
		}		
	}
	
	member:int DronesInSpace()
	{
		;echo "${EVE.GetEntityIDs[This.ActiveDroneIDList,OwnerID,${Me.CharID},CategoryID,${This.CategoryID_Drones}]}"
		return ${EVE.GetEntityIDs[This.ActiveDroneIDList,OwnerID,${Me.CharID},CategoryID,${This.CategoryID_Drones}]}
	}
	
	function GetDrones()
	{
		variable iterator Drone
		This.ActiveDroneIDList:GetIterator[Drone]
		
		if ${Drone:First(exists)}
			{
				do
				{		
					if ${Entity[${Drone.Value}].Distance} >= 501
					{
					Entity[${Drone.Value}]:Approach[500]
					echo "Approaching ${Entity[${Drone.Value}].Name}"
					}
					
					while ${Entity[${Drone.Value}].Distance} > 1500
					{
						wait 10
					}
						
					Entity[${Drone.Value}]:ScoopToDroneBay
					
					wait 10
				}
				while ${Drone:Next(exists)}
			}
	}

}